package com.handong.handserialprogram.module.guide;

import android.support.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.model.status.RingResponse;
import com.handong.handserialprogram.module.base.BasePresenter;
import com.handong.handserialprogram.module.base.BaseView;
import com.handong.handserialprogram.module.serialport.SerialPortUtil;
import com.handong.handserialprogram.utils.EventBusUtil;
import com.handong.handserialprogram.utils.JLog;
import com.handong.handserialprogram.utils.ViewUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by userKey on 2018/4/23.
 */

public class GuidePresenter extends BasePresenter<GuideActivity>{
    protected static final String TAG = GuidePresenter.class.getSimpleName();

    //private SerialPortUtil mSerialPortUtil;
    private  Timer timer;
    public GuidePresenter(BaseView view) {
        super(view);
        EventBusUtil.register(this);//EventBus
        //mSerialPortUtil = SerialPortUtil.getInstance(mView);
        //mSerialPortUtil.openSerialPortToReadData();
        timer=new Timer();
    }

//    public void sendIMEI(final String imei_ccr){
//        if(timer==null)return;
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                    mSerialPortUtil.sendIMEI();
//            }
//        },300, 300);
//    }


    /**
     * 接收脚环反馈
     * @param response
     */
    @Subscribe(priority = 100, threadMode = ThreadMode.MAIN)
    public void receiveRingResponse(RingResponse response) {
        switch (response.getResponseCode()) {
            case RingResponse.SEND_ID_SUCCESS://等待脚环连接
                if(timer!=null){
                    timer.cancel();
                    timer=null;
                    if (response.isSuccess()) {
                        mView.sendIdSuccess();
                    } else {
                        mView.sendIdFail();
                    }
                }

                break;
            default:
                return;
        }
    }

    public void errorDialog(){
        MaterialDialog dialog = new MaterialDialog.Builder(mView)
                .content("设备初始化出错，请立即重启！")
                .canceledOnTouchOutside(false)
                .positiveColorRes(R.color.back_ground_green)
                .positiveText(R.string.dialog_positive_text)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mView.finish();
                    }
                }).build();
        ViewUtil.removeAllKeyListener(dialog);
    }
    public void cancelDialog(){
        MaterialDialog dialog = new MaterialDialog.Builder(mView)
                .content("请同意申请的权限并继续使用")
                .canceledOnTouchOutside(false)
                .positiveColorRes(R.color.back_ground_green)
                .positiveText("确定")
                .negativeText("退出")
                .negativeColorRes(R.color.bg_gray)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mView.initApps();
                    }
                }).onNegative(new MaterialDialog.SingleButtonCallback() {
                    public void onClick(@NonNull MaterialDialog paramAnonymousMaterialDialog, @NonNull DialogAction paramAnonymousDialogAction) {
                        mView.finish();
                    }
                }).build();
        ViewUtil.removeAllKeyListener(dialog);
    }


    @Override
    protected void exit() {
        EventBusUtil.unRegister(this);
        //this.mSerialPortUtil.closeSerialPort();
        super.exit();
        JLog.d(TAG, "Active Data:" + "exit");
    }

}
